package com.hisoft.entity;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.log4j.Logger;

import com.hisoft.selenium.DlinkXMLDriveSelenium;
import com.hisoft.type.ResultEnum;
import com.hisoft.utility.CommonTool;

/**
 * 
 * @author COOLOO
 * 
 */
public abstract class ABaseTag {
	// selenium drvie
	Object xmlDriveSelenium = DlinkXMLDriveSelenium.getInstance();
	// 操作信息
	private StringBuffer sb = new StringBuffer();
	// 自定义信息
	private String info = "";
	// 所在XML行数
	private int lineNumb = 0;
	// 操作结果
	private ResultEnum result = ResultEnum.NOTRUN;
	Logger logger = Logger.getLogger(ABaseTag.class);
	// 截图路径
	private String screenshot = "";

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public String getScreenshot() {
		return screenshot;
	}

	public void setScreenshot(String screenshot) {
		logger.info("screenshot: " + screenshot);
		this.screenshot = screenshot;
	}

	public ABaseTag() {
	}

	public String getTag() {
		return "ABaseTag";
	}

	public ResultEnum getResult() {
		return result;
	}
	/**
	 * 如果result的结果为fail，那么就截图，顺便把图片地址保存下来
	 * @param result
	 */
	public void setResult(ResultEnum result) {
		if (result.equals(ResultEnum.FAIL)) {
			try {
				setScreenshot(reflectMethod("captureScreenshotPath", "", ""));
			} catch (SecurityException e) {
				logger.error(e.getMessage(), e);
			} catch (IllegalArgumentException e) {
				logger.error(e.getMessage(), e);
			} catch (NoSuchMethodException e) {
				logger.error(e.getMessage(), e);
			} catch (IllegalAccessException e) {
				logger.error(e.getMessage(), e);
			} catch (InvocationTargetException e) {
				logger.error(e.getMessage(), e);
			}
		}
		this.result = result;
	}

	/**
	 * 把对象状态从NOTRUN改成FAIL。
	 * 
	 * <b></b><br/>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-8-30
	 */
	public void beforeExcute() {
		this.result = ResultEnum.FAIL;
	}

	public int getLineNumb() {
		return lineNumb;
	}

	public void setLineNumb(int lineNumb) {
		this.lineNumb = lineNumb;
	}

	public String getReportInfo() {
		return sb.toString();
	}

	public void append(String str) {
		sb.append(str);
	}
	
	//执行该步骤
	public String excute() {
		return null;
	}
	/**
	 * 方法发射，用来根据方法名字条用相应的方法
	 * @param command
	 * @param target
	 * @param value
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public String reflectMethod(String command, String target, String value) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		String returnStr = "" + ResultEnum.FAIL;
		try {
			Method f = xmlDriveSelenium.getClass().getMethod(command, new Class[] { String.class });
			returnStr = CommonTool.any2String(f.invoke(xmlDriveSelenium, target));
		} catch (NoSuchMethodException e) {
			try {
				Method f = xmlDriveSelenium.getClass().getMethod(command, new Class[] { String.class, String.class });
				returnStr = CommonTool.any2String(f.invoke(xmlDriveSelenium, target, value));
			} catch (NoSuchMethodException e1) {
				Method f = xmlDriveSelenium.getClass().getMethod(command, new Class[] {});
				returnStr = CommonTool.any2String(f.invoke(xmlDriveSelenium));
			}
		}
		return returnStr;
	}
	
	/**
	 * 克隆本个对象
	 */
	public ABaseTag clone() {
		return null;
	}
}
